package cn.tedu._07weibo.controller;

import cn.tedu._07weibo.base.response.JsonResult;
import cn.tedu._07weibo.base.response.StatusCode;
import cn.tedu._07weibo.mapper.CommentMapper;
import cn.tedu._07weibo.mapper.WeiboMapper;
import cn.tedu._07weibo.pojo.dto.CommentAddParam;
import cn.tedu._07weibo.pojo.entity.Comment;
import cn.tedu._07weibo.pojo.vo.CommentVO;
import cn.tedu._07weibo.pojo.vo.UserVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("v1/comment/")
@Slf4j
@Tag(name = "评论模块")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;


    @PostMapping("insert")
    @Operation(summary = "发表评论")
    @ApiOperationSupport(order = 10)
    public JsonResult insertComment(@RequestBody CommentAddParam commentAddParam, HttpSession session){
        UserVo user =(UserVo) session.getAttribute("user");
        if(user==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Comment comment=new Comment();
        BeanUtils.copyProperties(commentAddParam,comment);
        comment.setUserId(user.getId());
        comment.setCreateTime(new Date());
        int rows = commentMapper.insertCommentAdd(comment);
        if(rows==0){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }return JsonResult.ok();
    }

    @GetMapping("selectByWeiboId")
    @Operation(summary = "微博评论列表")
    @ApiOperationSupport(order = 30)
    public JsonResult selectComment(Long wid){
        List<CommentVO> listc=commentMapper.selectWeiboCommentBywid(wid);
        if(listc==null){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }return JsonResult.ok(listc);


    }
}
